/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.data.crud;

import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;

/**
 *
 * @author Thiago Belotto
 */
public class CrudUsuario {

    EntityManagerFactory emf = Persistence.createEntityManagerFactory("SistemaGoNowPU");

    public void persist(Object object) {
        EntityManager em = emf.createEntityManager();
        try {
            em.getTransaction().begin();
            em.persist(object);
            em.getTransaction().commit();
        } catch (Exception e) {
            Logger.getLogger(getClass().getName()).log(Level.SEVERE, "exception caught", e);
            em.getTransaction().rollback();
        } finally {
            em.close();
        }
    }

    public void persist(String nome, int ra, int seap, String sexo, String telefone, String email, Date data_nas, String senha, int id_tipo_usuario) {
        EntityManager em = emf.createEntityManager();
        try {
            br.data.model.Usuario usu;
            usu = new br.data.model.Usuario();
            usu.setRa(ra);
            usu.setSeap(seap);
            usu.setNome(nome);
            usu.setTelefone(telefone);
            usu.setSexo(sexo);
            usu.setDataNasc(data_nas);
            usu.setEmail(email);
            usu.setSenha(senha);
            usu.setIdTipoUsuario(new br.data.crud.CrudTipoUsuario().getTipo(id_tipo_usuario));
            em.getTransaction().begin();
            em.persist(usu);
            em.getTransaction().commit();
        } catch (Exception e) {
            Logger.getLogger(getClass().getName()).log(Level.SEVERE, "exception caught", e);
            em.getTransaction().rollback();
        } finally {
            em.close();
        }
    }

    public void update(int id, String nome, int ra, int seap, String sexo, String telefone, String email, Date data_nas, String senha, int id_tipo_usuario) {


        EntityManager em = emf.createEntityManager();
        try {
            em.getTransaction().begin();
            br.data.model.Usuario usu;
            usu = em.find(br.data.model.Usuario.class, id);
            usu.setRa(ra);
            usu.setSeap(seap);
            usu.setNome(nome);
            usu.setTelefone(telefone);
            usu.setSexo(sexo);
            usu.setDataNasc(data_nas);
            usu.setEmail(email);
            usu.setSenha(senha);
            usu.setIdTipoUsuario(new br.data.crud.CrudTipoUsuario().getTipo(id_tipo_usuario));

            em.merge(usu);
            em.getTransaction().commit();
        } catch (Exception e) {
            Logger.getLogger(getClass().getName()).log(Level.SEVERE, "exception caught", e);
            em.getTransaction().rollback();
        } finally {
            em.close();
        }
    }

    public void remove(int codigo) {
        EntityManager em = emf.createEntityManager();
        try {
            em.getTransaction().begin();
            br.data.model.Usuario usu;
            usu = em.find(br.data.model.Usuario.class, codigo);
            em.remove(usu);
            em.getTransaction().commit();
        } catch (Exception e) {
            Logger.getLogger(getClass().getName()).log(Level.SEVERE, "exception caught", e);
            em.getTransaction().rollback();
        } finally {
            em.close();
        }
    }

    public java.util.Collection<br.data.model.Usuario> getall() {
        EntityManager em = emf.createEntityManager();
        try {
            return em.createNamedQuery("Usuario.findAll").getResultList();
        } catch (Exception e) {
            return null;
        } finally {
            em.close();
        }
    }

    public br.data.model.Usuario getUsu(int codigo) {

        EntityManager em = emf.createEntityManager();
        try {
            return em.find(br.data.model.Usuario.class, codigo);

        } catch (Exception e) {
            return null;
        } finally {
            em.close();
        }
    }
    
       public Object log(String email, String senha) {

        EntityManager em = emf.createEntityManager();
        try {
            em.getTransaction().begin();
            return em.createNamedQuery("Usuario.findByEmailSenha").
                    setParameter("email", email).setParameter("senha", senha).getSingleResult();
            /*@NamedQuery(name = "Usuario.findByEmailSenha", query = "SELECT t FROM Usuario u WHERE u.email = :email AND u.senha = :senha")}) */
        } catch (Exception e) {
            Logger.getLogger(getClass().getName()).log(Level.SEVERE, "exception caught", e);
            em.getTransaction().rollback();
            return null;
        } finally {
            em.close();
        }

    }
    
}
